<?php

include '../../../wp-blog-header.php';
$brand_id = '822';
//$url = 'http://www.bagittoday.com/getProductInfo.srv?appId=100001&type=1&tagId=36-38-43-2';
$url = 'http://www.bagittoday.com/getProductInfo.srv?appId=100001&type=1&tagId=1-2-3-4-5-14-15-24-25-30-31-33-32-34-35-36-37-38-39-40-41-42-43-44';


function download_page($path){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$path);
        curl_setopt($ch, CURLOPT_FAILONERROR,1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
        $retValue = curl_exec($ch);                      
        curl_close($ch);
        return $retValue;
}

$curl_json_output = download_page($url);
$bagit_deals = xml2array($curl_json_output);

shuffle($bagit_deals['root']['productInfo']);
$bagit_final = array_slice($bagit_deals['root']['productInfo'], 0, 1);


foreach ($bagit_final as $key=>$value){
  $deal_link = 'http://www.bagittoday.com/details.htm?offerId='.$value['id'].'&BCODE=C1525';
  $name = strip_tags(trim(substr($value['shortDesc'],0,120)));
  //$deal_title = $value['title'];
  $image = 'http://bagittoday.com'.$value['imagePath'][0];
  $orig = $value['previousPrice'];
  $new = $value['currentPrice'];
  
  $columns_deal['name'] = pods_sanitize(ucwords(strtolower($name)));
  $columns_deal['camp_link'] = substr(sanitize_title($columns_deal['name']),0,128);
    
  $deal = new Pod('deal');
  $deal->page=1;
  $deal->search=false;
  $where = "t.camp_link = '".$columns_deal['camp_link']."' AND t.daily = 1";
  $deal->findRecords('id DESC', 1,$where);
  $dealsFound = $deal->getTotalRows();
    
    if($dealsFound == 0)
    {
      unset($current_cities);
      $City = new Pod('city');
    	$City->findRecords('name ASC', 1000);
    	while($City->fetchRecord())
    	{
      	$ids = $City->get_field('id');
      	$current_cities[] = $ids;
    	}
      $current_cities = implode(',',$current_cities);
      $columns_deal['city'] = $current_cities;
      
      $shortened_url = make_bitly_url($deal_link);
      if(empty($shortened_url))
      {
        $shortened_url = make_goog_url($deal_link);
      }
      $columns_deal['extlink'] = $shortened_url;
      
      $columns_deal['camp_end'] = date('Y-m-d 22:00:00',strtotime('+2 day'));
      $columns_deal['camp_start'] = date('Y-m-d H:i:s');
      $columns_deal['brand'] = $brand_id;
      $columns_deal['daily'] = '1';
      $columns_deal['oriprice'] = $orig;
      $columns_deal['disprice'] = $new;//you pay
    
      $columns_deal['disper'] = floor(((($columns_deal['oriprice'] - $columns_deal['disprice'])/$columns_deal['oriprice'])*100));
      
      $columns_deal['ext_image_url'] = $image;
    
      $columns_deal['camp_cat'] = '29';//Fashion
      
      $api = new PodAPI();
      $params = array('datatype' => 'deal', 'columns' => $columns_deal);
    
      /*
      echo '<pre>';
      print_r($params);
      echo '</pre>';
      exit();
      */
      $api->save_pod_item($params);
    }
   
 }
?>